import { reqLogin, reqGetUserInfo, reqGetUserDetailById } from '@/api/user'
import { setToken, getToken, removeToken } from '@/utils/auth'
export default {
  namespaced: true,
  state: {
    token: getToken() || '',
    userInfo: {}
  },
  mutations: {
    steToken(state, newToken) {
      state.token = newToken
      setToken(newToken)
    },
    steUserInfo(state, newUserInfo) {
      state.userInfo = newUserInfo
    },
    removeUserInfo(state) {
      state.token = null
      removeToken()
      state.userInfo = {}
    }
  },
  actions: {
    async login(ctx, data) {
      const { data: token } = await reqLogin(data)
      ctx.commit('steToken', token)
    },
    async getUserInfo({ commit }) {
      const { data: data1 } = await reqGetUserInfo()
      const { data: data2 } = await reqGetUserDetailById(data1.userId)
      const baseResult = Object.assign(data1, data2)
      console.log(baseResult)
      commit('steUserInfo', baseResult)
    },
    logout({ commit }) {
      commit('removeUserInfo')
    }
  }
}
